package com.sc.study.student.controller;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.sc.common.annotation.Excel;
import com.sc.study.clazz.service.IScClazzService;
import com.sc.study.foundation.service.IScGradeService;
import com.sc.study.foundation.service.IScSubjectService;
import com.sc.study.student.service.IScStudentClazzService;
import com.sc.study.student.service.IScStudentSubjectService;
import com.sc.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sc.common.annotation.Log;
import com.sc.common.core.controller.BaseController;
import com.sc.common.core.domain.AjaxResult;
import com.sc.common.enums.BusinessType;
import com.sc.study.student.domain.ScStudent;
import com.sc.study.student.service.IScStudentService;
import com.sc.common.utils.poi.ExcelUtil;
import com.sc.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学员Controller
 *
 * @author sc
 * @date 2022-02-27
 */
@RestController
@RequestMapping("/study/student")
public class ScStudentController extends BaseController {
    private static final String EQUALSTO = "= ";
    @Autowired
    private IScStudentService scStudentService;
    @Autowired
    private IScSubjectService scSubjectService;
    @Autowired
    private IScGradeService scGradeService;
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private IScClazzService scClazzService;
    @Autowired
    private IScStudentClazzService scStudentClazzService;
    @Autowired
    private IScStudentSubjectService scStudentSubjectService;


    /**
     * 根据id集合查询学员列表
     */
    @PreAuthorize("@ss.hasPermi('study:student:list')")
    @PostMapping("/find")
    public AjaxResult find(@RequestBody List<Integer> ids) {
        List<ScStudent> list = ids.stream().map(id-> scStudentService.selectScStudentById(id.longValue())).collect(Collectors.toList());
        return AjaxResult.success(list);
    }


    @GetMapping("/getOne/{id}")
    public AjaxResult getOne(@PathVariable Long id) {
        return AjaxResult.success(scStudentService.getOne(id));
    }

    /**
     *  根据学生code查询未绑定寝室的学生数据
     *  该学生绑定寝室， 抛出异常
     * @param stuCode
     * @return
     */
    @GetMapping("/getByCode/{stuCode}")
    public AjaxResult getByCode(@PathVariable Long stuCode) {
        return AjaxResult.success(scStudentService.getByCode(stuCode));
    }


    /**
     * 查询可参加此次考试的学生
     */
    @PreAuthorize("@ss.hasPermi('study:student:list')")
    @GetMapping("/listCanJoinExam")
    public AjaxResult listCanJoinExamStudent(ScStudent scStudent) {
        return AjaxResult.success(scStudentService.listCanJoinExamStudent(scStudent));
    }

    /**
     *
     * 根据班级查询学生，有寝室的绑定寝室
     * @param scStudent
     * @return
     */
    @GetMapping("/listCanJoinSleepRoom")
    public TableDataInfo listCanJoinSleepRoom(ScStudent scStudent) {
        startPage();
        return getDataTable(scStudentService.listCanJoinSleepRoom(scStudent));
    }

    /**
     *
     * 查询没有绑定寝室的学生
     * @param scStudent
     * @return
     */
    @GetMapping("/listCanNotJoinSleepRoom")
    public AjaxResult listCanNotJoinSleepRoom(ScStudent scStudent) {
        return AjaxResult.success(scStudentService.listCanNotJoinSleepRoom(scStudent));
    }


    /**
     * 查询绑定本次考试的学生
     * @param scStudent 考试编号
     * @return
     */
    @GetMapping("/listBindExam")
    public AjaxResult listBindExam(ScStudent scStudent) {
        return AjaxResult.success(scStudentService.listBindExam(scStudent));
    }

    /**
     * 查询未被分班的学员列表
     */
    @PreAuthorize("@ss.hasPermi('study:student:list')")
    @GetMapping("/listNoClazz")
    public AjaxResult listNoClazz(ScStudent scStudent) {
        return AjaxResult.success(scStudentService.selectScStudentNoClazz(scStudent));
    }


    /**
     * 查询学员列表包含班级
     */
    @PreAuthorize("@ss.hasPermi('study:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScStudent scStudent) {
        startPage();
        List<ScStudent> list = scStudentService.selectScStudentIncludeClazz(scStudent);
        return getDataTable(list);
    }

    /**
     * 查询学员列表包含班级不分页
     */
    @PreAuthorize("@ss.hasPermi('study:student:list_no_page')")
    @GetMapping("/listNoPage")
    public AjaxResult listNoPage(ScStudent scStudent) {
        List<ScStudent> list = scStudentService.selectScStudentIncludeClazz(scStudent);
        return AjaxResult.success(list);
    }

    /**
     * 导出学员列表
     */
    @PreAuthorize("@ss.hasPermi('study:student:export')")
    @Log(title = "学员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScStudent scStudent) {
        List<ScStudent> list = scStudentService.selectScStudentList(scStudent);
        ExcelUtil<ScStudent> util = new ExcelUtil<ScStudent>(ScStudent.class);
        util.exportExcel(response, list, "学员数据");
    }

    /**
     * 下载学员列表模板（在学员列表中的模板）
     */
    @PreAuthorize("@ss.hasPermi('study:student:importExcelModel')")
    @Log(title = "学员", businessType = BusinessType.IMPORT)
    @PostMapping("/importExcelModel")
    public void importExcelModel(HttpServletResponse response) {
        ExcelUtil<ScStudent> excelUtil = new ExcelUtil<>(ScStudent.class);
        excelUtil.init(null, "学员列表模板", null, Excel.Type.EXPORT);

        // 设置小提示
        excelUtil.setXSSFPrompt(excelUtil.getSheet(), "tips", "注意编码不能重复", 1, 100, 1, 1);
        excelUtil.setXSSFPrompt(excelUtil.getSheet(), "tips", "手机号码请输入11位", 1, 100, 4, 4);
        excelUtil.getSheet().setColumnHidden(2, true);
        excelUtil.exportExcel(response);
    }

    /**
     * 导入学员列表
     * @param file excel
     * @param clazzId 如果传入了班级编号，就代表在该班级里导入学员
     * @throws IOException
     */
    @PreAuthorize("@ss.hasPermi('study:student:importExcel')")
    @Log(title = "学员", businessType = BusinessType.IMPORT)
    @PostMapping("/importExcel")
    public void importExcel(MultipartFile file,Long clazzId) throws IOException {
        scStudentService.importExcel(file, clazzId);
    }


    /**
     * 获取学员详细信息
     */
    @PreAuthorize("@ss.hasPermi('study:student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(scStudentService.selectScStudentById(id));
    }

    /**
     * 新增学员
     */
    @PreAuthorize("@ss.hasPermi('study:student:add')")
    @Log(title = "学员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScStudent scStudent) {
        return toAjax(scStudentService.insertScStudent(scStudent));
    }

    /**
     * 修改学员
     */
    @PreAuthorize("@ss.hasPermi('study:student:edit')")
    @Log(title = "学员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScStudent scStudent) {
        return toAjax(scStudentService.updateScStudent(scStudent));
    }


    /**
     * 批量删除学员
     */
    @PreAuthorize("@ss.hasPermi('study:student:remove')")
    @Log(title = "学员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(scStudentService.deleteScStudentByIds(ids));
    }

    /**
     * 删除学员
     */
    @PreAuthorize("@ss.hasPermi('study:student:removeStudent')")
    @Log(title = "学员", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeStudent")
    public AjaxResult removeStudent(@RequestBody ScStudent scStudent) {
        return toAjax(scStudentService.removeStudent(scStudent));
    }

    /**
     * 获取学员状态
     */
    @PreAuthorize("@ss.hasPermi('study:student:query')")
    @GetMapping(value = "getStudentState")
    public AjaxResult getStudentState(){
        return AjaxResult.success(sysConfigService.selectConfigByKey("student_state"));
    }

}
